package com.atguigu.gmall.product.rpc;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.entity.SkuImage;
import com.atguigu.gmall.model.product.entity.SkuInfo;
import com.atguigu.gmall.model.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.service.SkuImageService;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/api/inner/product")
public class SkuDetailController {

    @Autowired
    SkuInfoService skuInfoService;

    @Autowired
    SkuImageService skuImageService;


    @Autowired
    SpuSaleAttrService spuSaleAttrService;


    @GetMapping("/getSkuInfo/{skuId}")
    public Result getSkuInfo(@PathVariable Long skuId) {
        SkuInfo skuInfo = skuInfoService.getById(skuId);
        return Result.ok(skuInfo);
    }

    @GetMapping("/getSkuImgs/{skuId}")
    public Result getSkuImgs(@PathVariable Long skuId) {
        List<SkuImage> skuImageList = skuImageService.lambdaQuery()
                .eq(SkuImage::getSkuId, skuId).list();
        return Result.ok(skuImageList);
    }

    @GetMapping("/getPrice/{skuId}")
    public Result<BigDecimal> getPrice(@PathVariable Long skuId) {
        SkuInfo skuInfo = skuInfoService.lambdaQuery()
                .select(SkuInfo::getPrice)
                .eq(SkuInfo::getId, skuId)
                .one();
        return Result.ok(skuInfo.getPrice());
    }


    @GetMapping("/getSpuSaleAttrListBySpuIdAndSkuId/{skuId}/{spuId}")
    public Result<List<SpuSaleAttr>> getSpuSaleAttrListBySpuIdAndSkuId(@PathVariable Long skuId
            , @PathVariable Long spuId
    ) {
        List<SpuSaleAttr> spuSaleAttrList = spuSaleAttrService.getSpuSaleAttrListBySpuIdAndSkuId(skuId, spuId);
        return Result.ok(spuSaleAttrList);
    }


    @GetMapping("/getValuesSkuJsonTO/{spuId}")
    public Result<String> getValuesSkuJsonTO(@PathVariable Long spuId) {
        String res = spuSaleAttrService.getValuesSkuJsonTO(spuId);
        return Result.ok(res);
    }


}
